package com.brt.museum.setup;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.util.Daos;
import org.nutz.ioc.Ioc;
import org.nutz.lang.Lang;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;

import com.brt.museum.bean.auth.Permission;
import com.brt.museum.bean.auth.Role;
import com.brt.museum.bean.auth.RolePermission;
import com.brt.museum.bean.auth.User;
import com.brt.museum.bean.auth.User.Status;
import com.brt.museum.bean.auth.UserRole;
import com.brt.museum.dao.auth.IRolePermissionDao;
import com.brt.museum.dao.auth.IUserRoleDao;
import com.brt.museum.dao.auth.impl.RolePermissionDao;
import com.brt.museum.dao.auth.impl.UserRoleDao;
import com.brt.museum.service.auth.IPermissionService;
import com.brt.museum.service.auth.IRoleService;
import com.brt.museum.service.auth.IUserService;
import com.brt.museum.service.auth.impl.PermissionService;
import com.brt.museum.service.auth.impl.RoleService;
import com.brt.museum.service.auth.impl.UserService;
import com.brt.museum.vo.Consts.InstalledPermission;
import com.brt.museum.vo.Consts.InstalledRoles;
import com.dgj.quartz.NutQuartzCronJobFactory;
import com.dgj.utils.encode.DES;

/**
 * @author 王贵源
 * @email kerbores@dgj.com
 *
 *        create at 2015年10月20日 上午9:56:41
 */
public class ApplicationSetup implements Setup {

	private Log log = Logs.get();

	Dao dao = Mvcs.getIoc().get(Dao.class);

	IUserService userService = Mvcs.getIoc().get(UserService.class);

	IPermissionService permissionService = Mvcs.getIoc().get(PermissionService.class);

	IRoleService roleService = Mvcs.getIoc().get(RoleService.class);

	IUserRoleDao userRoleDao = Mvcs.getIoc().get(UserRoleDao.class);

	IRolePermissionDao rolePermissionDao = Mvcs.getIoc().get(RolePermissionDao.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nutz.mvc.Setup#destroy(org.nutz.mvc.NutConfig)
	 */
	@Override
	public void destroy(NutConfig config) {
		System.exit(0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nutz.mvc.Setup#init(org.nutz.mvc.NutConfig)
	 */
	@Override
	public void init(NutConfig config) {
		log.debug("setup application...");
		Ioc ioc = config.getIoc();
		ioc.get(NutQuartzCronJobFactory.class);
		Dao dao = config.getIoc().get(Dao.class);
		Daos.createTablesInPackage(dao, "com.brt.museum", false);
		Daos.createTablesInPackage(dao, "com.dgj.utils.db.data", false);
		initData();
	}

	/**
	 * 
	 */
	private void initData() {
		// 平台管理员
		User user = userService.findByName("admin");
		if (user == null) {
			user = new User();
			user.setName("admin");
			user.setRealName("平台管理员");
			user.setPassword(DES.encrypt("admin", Lang.md5("admin")));
			user.setStatus(Status.ACTIVED);
			userService.addUser(user);
		}
		// 内置角色
		for (InstalledRoles installedRoles : InstalledRoles.values()) {
			if (roleService.findByName(installedRoles.getName()) == null) {
				roleService.addRole(installedRoles.toRole());// 保存
			}
		}
		// 给用户admin设置admin角色
		Role admin = roleService.findByName(InstalledRoles.ADMIN.getName());
		if (userRoleDao.findByCondition(Cnd.where("userId", "=", user.getId()).and("roleId", "=", admin.getId())) == null) {
			UserRole ur = new UserRole();
			ur.setRoleId(admin.getId());
			ur.setUserId(user.getId());
			userRoleDao.save(ur);
		}
		// 内置权限
		for (InstalledPermission installedPermission : InstalledPermission.values()) {
			Permission temp = null;
			if ((temp = permissionService.findByName(installedPermission.getName())) == null) {
				temp = permissionService.addPermission(installedPermission.toPermission());
			}
			// 给admin角色设置全部权限
			if (rolePermissionDao.findByCondition(Cnd.where("permissionId", "=", temp.getId()).and("roleId", "=", admin.getId())) == null) {
				RolePermission rp = new RolePermission();
				rp.setPermissionId(temp.getId());
				rp.setRoleId(admin.getId());
				rolePermissionDao.save(rp);
			}
		}

	}

}
